/*
 *  Copyright (C) 2010 Glauco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components.panel.layouts;

import java.util.ArrayList;
import java.util.List;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.JComponent;

/**
 *
 * @author Glauco
 */
public class BaliComponentsGroup {

    List<BaliComponentsRow> rows;
    GroupLayout layout;

    public BaliComponentsGroup() {
        rows = new ArrayList<BaliComponentsRow>();
    }

    public BaliComponentsGroup(GroupLayout layout) {
        this();
        this.layout = layout;
    }

    public void addComponentsRow(BaliComponentsRow row){
        this.rows.add(row);
    }

    public ParallelGroup getHorizontalGroup(GroupLayout layout){
        GroupLayout.SequentialGroup group = layout.createSequentialGroup();

        // Grupo dos Labels
        ParallelGroup labelsGroup = layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING);

        // Grupo dos Componentes
        ParallelGroup componentsGroup = layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING);

        // para cada linha, monta os grupos
        for (BaliComponentsRow row : rows) {

            labelsGroup.addComponent(row.getFirstColumnComponent());

            if (row.getComponentesSize() > 0)
                if (row.getComponentesSize() > 1)
                    componentsGroup.addGroup(row.getGroupOfComponentes(layout));
                else
                    componentsGroup.addComponent(
                            row.getFirstComponentOfSecondColumn(),
                            GroupLayout.PREFERRED_SIZE,
                            GroupLayout.DEFAULT_SIZE,
                            GroupLayout.PREFERRED_SIZE);
        }

        // Gap Inicial
        group.addContainerGap();

        // adiciona o grupo dos labels
        group.addGroup(labelsGroup);

        // Gap Intermediário
        group.addPreferredGap(
                javax.swing.LayoutStyle.ComponentPlacement.RELATED);

        // adicional o grupo dos componentes
        group.addGroup(componentsGroup);

        // Gap Final
        group.addContainerGap(
                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE);

        return layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(group);
    }

    public ParallelGroup getVerticalGroup(GroupLayout layout){
        GroupLayout.SequentialGroup group = layout.createSequentialGroup();

        // Gap Inicial
        group.addContainerGap();

        boolean gap = false;

        // Para cada linha, adicional os gropos
        for (BaliComponentsRow row : rows) {
            if (gap)
                group.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED);
            else
                gap = true;

            group.addGroup(row.getBaselineGroup(layout));
        }

        // Gap Final
        group.addContainerGap(GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE);

        return layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(group);
    }

    public void buildUniqueComponentLayoutGroup(){
        buildUniqueComponentLayoutGroup(layout);
    }

    public void buildUniqueComponentLayoutGroup(GroupLayout layout){

        if (layout == null)
            return;

        if (rows.isEmpty())
            return;

        JComponent component = rows.get(0).getFirstColumnComponent();

        layout.setHorizontalGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(component)
                .addContainerGap());

        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
            .addComponent(component));
    }
}
